package com.kly.cashmall.module.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import com.kly.cashmall.services.UserInfoHelper;

import java.io.Serializable;

/**
 * @author
 * @date 2018/1/16
 */
public class LoginIntent {

  private Intent intent;
  private Context context;

  private LoginIntent(Context context, @NonNull Intent intent) {
    this.intent = intent;
    this.context = context;
  }

  /**
   * 构造一个需要页面，通用型
   */
  public static LoginIntent with(Context context, Intent intent) {
    return new LoginIntent(context, intent);
  }

  /**
   * 构造一个需要登录的普通页面
   */
  public static LoginIntent withClass(Context context, Class<? extends Activity> clazz) {
    return new LoginIntent(context, new Intent(context, clazz));
  }


  public LoginIntent put(String key, Serializable value) {
    intent.putExtra(key, value);
    return this;
  }

  public LoginIntent put(String key, Parcelable value) {
    intent.putExtra(key, value);
    return this;
  }

  public LoginIntent put(String key, String value) {
    intent.putExtra(key, value);
    return this;
  }

  public LoginIntent put(String key, int value) {
    intent.putExtra(key, value);
    return this;
  }

  public LoginIntent put(String key, float value) {
    intent.putExtra(key, value);
    return this;
  }

  public LoginIntent put(String key, boolean value) {
    intent.putExtra(key, value);
    return this;
  }

  public LoginIntent put(String key, double value) {
    intent.putExtra(key, value);
    return this;
  }

  public LoginIntent put(String key, Bundle value) {
    intent.putExtra(key, value);
    return this;
  }

  public LoginIntent put(Bundle value) {
    if (value != null) {
      intent.putExtras(value);
    }
    return this;
  }

  /**
   * 最终获取Intent
   */
  public Intent get() {
    if (UserInfoHelper.getInstance().isLogin()) {
      return intent;
    } else {
      return LoginRedirectHelper.setRedirectData(context, LoginActivity.class, intent.getExtras(),
          intent.getComponent() == null ? "" : intent.getComponent().getClassName(), "");
    }
  }
}